﻿using System;
using System.Globalization;
using Microsoft.CSharp;
using DslModeling=global::Microsoft.VisualStudio.Modeling;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using DslValidation=global::Microsoft.VisualStudio.Modeling.Validation;

namespace Salamanca.Saml
{
    public interface INamedElement
    {
        string Name { get; set; }
    }

    internal static class NamedElement
    {
        /// <summary>Ensures that names will be valid C# identifiers.</summary>
        public static void ValidateName(DslValidation.ValidationContext context, INamedElement element)
        {
            if (string.IsNullOrEmpty(element.Name))
            {
                context.LogError(
                    Resources.SR.ElementMustHaveName,
                    "SAML01",
                    (DslModeling.ModelElement)element
                );
                return;
            }

            if (!(new CSharpCodeProvider().IsValidIdentifier(element.Name)))
                context.LogError(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.SR.InvalidName,
                        element.Name
                    ),
                    "SAML01",
                    (DslModeling.ModelElement)element
                );
        }
    }
}
